package com.crk.yiyuanguahao.service.impl;

import com.crk.yiyuanguahao.dao.OrgDao;
import com.crk.yiyuanguahao.entity.Org;
import com.crk.yiyuanguahao.entity.OrgDto;
import com.crk.yiyuanguahao.entity.User;
import com.crk.yiyuanguahao.service.OrgService;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseEnum;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/3/26
 */
@Service
public class OrgServiceImpl implements OrgService {
    @Autowired
    OrgDao orgDao;


    @Override
    public ResponseResult saveOrg(Org org) {
        if (null != org){
            int result = 0;
            if (org.getId() == null){
                User user = (User) SecurityUtils.getSubject().getPrincipal();
                org.setCreator(user.getId());
                org.setUpdator(user.getId());
                org.setCreateTime(new Date());
                org.setUpdateTime(new Date());
                result = orgDao.insert(org);
            }else{
                User user = (User) SecurityUtils.getSubject().getPrincipal();
                org.setUpdator(user.getId());
                org.setUpdateTime(new Date());
                result = orgDao.updateByPrimaryKey(org);
            }

            if (result > 0){
                return ResponseResult.success(org);
            }else{
                return ResponseResult.fail();
            }
        }else {
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult deleteOrg(Integer id) {
        int deleteByPrimaryKey = orgDao.deleteByPrimaryKey(id);
        if(deleteByPrimaryKey > 0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult getOrg(Integer id) {
        Org org = orgDao.selectByPrimaryKey(id);
        return ResponseResult.success(org);
    }

    @Override
    public ResponseResult queryOrgPage(String orgName, Page pageInfo) {
        Integer count = orgDao.countByCondition(orgName);
        pageInfo.setAllRow(count);
        List<Org> orgList = orgDao.selectByCondition(orgName, pageInfo.getStart(), pageInfo.getSize());
        List<OrgDto> orgDtos = new ArrayList<>();
        for (Org org : orgList) {
            OrgDto orgDto = new OrgDto();
            if (org.getParentOrg() == -1){
                orgDto.setParentName("医院");
            }else{
                Org selectByPrimaryKey = orgDao.selectByPrimaryKey(org.getParentOrg());
                if (null != selectByPrimaryKey){
                    orgDto.setParentName(selectByPrimaryKey.getOrgName());
                }else{
                    orgDto.setParentName("");
                }
            }
            orgDto.setId(org.getId());
            orgDto.setOrgName(org.getOrgName());
            orgDto.setCreateTime(org.getCreateTime());
            orgDto.setUpdateTime(org.getUpdateTime());
            orgDtos.add(orgDto);
        }
        return ResponseResult.success(orgDtos);
    }

    @Override
    public List<Org> queryOrgAll() {
        return orgDao.selectAll();
    }

    @Override
    public ResponseResult getOrgChildrenList() {
        List<Org> orgList = orgDao.selectOrgByParent(-1);
        List<OrgDto> orgDtoList = new ArrayList<>();
        for (Org org : orgList) {
            OrgDto orgDto = new OrgDto();
            orgDto.setId(org.getId());
            orgDto.setOrgName(org.getOrgName());
            List<Org> children = orgDao.selectOrgByParent(org.getId());
            orgDto.setChildren(children);
            orgDtoList.add(orgDto);
        }
        return ResponseResult.success(orgDtoList);
    }
}
